package com.test.jbpm.dao.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.test.jbpm.Pager;
import com.test.jbpm.dao.StudentDao;
import com.test.jbpm.domain.Student;
import com.test.jbpm.domain.User;


@Repository
public class StudentDaoImpl extends BaseDao implements StudentDao {

	public void addOrUpdate(Student student) {
		getHibernateTemplate().saveOrUpdate(student);
	}

	public void del(Integer studentId) {
		getHibernateTemplate().delete(
			getHibernateTemplate().load(Student.class, studentId)	
		);
	}

	public Student findStudentById(Integer studentId) {
		return (Student)getHibernateTemplate().load(Student.class, studentId);
	}

	@SuppressWarnings("unchecked")
	public Pager<Student> findStudentList() {
		return findData("from Student");
	}

	public User login(final String userCode, final String password) {
		return (User)getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				return session.createQuery("from User u where u.code=? and u.password=?")
							.setParameter(0, userCode)
							.setParameter(1, password)
							.uniqueResult();
			}
		});
	}

}
